package view;
/**
 *
 * @author zhaoyingxi
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;


import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumnModel;
import java.util.List;

import model.User;
import Control.UserControl;
/**
*@author zhaoyingxi jc11839209
*/
public class UserManagerViewT {
    private JFrame frame = null;

    private JTable table = null;

    private UserManagerTable_Model model = null;

    private JScrollPane s_pan = null;

    private JButton button_1 = null, button_2 = null, button_3 = null;

    private JPanel pane = null;
    public UserManagerViewT() {
        List<User> list = UserControl.find();
        frame = new JFrame("UserManagerView");
        pane = new JPanel();
        button_1 = new JButton("Delete");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                removeData();
            }
        });
        button_2 = new JButton("ADD");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addUser();
            }
        });
        button_3 = new JButton("SAVE");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                saveData();
            }
        });
        pane.add(button_1);
        pane.add(button_2);
        pane.add(button_3);
        model = new UserManagerTable_Model(20);

        table = new JTable(model);
        //
        table.setBackground(Color.white);
        //
//        String[] age = { "16", "17", "18", "19", "20", "21", "22" };
//        JComboBox com = new JComboBox(age);

        TableColumnModel tcm = table.getColumnModel();
//        tcm.getColumn(3).setCellEditor(new DefaultCellEditor(com));

        tcm.getColumn(0).setPreferredWidth(50);
        tcm.getColumn(1).setPreferredWidth(100);
        tcm.getColumn(2).setPreferredWidth(50);
        for(int i=0;i<list.size();i++)
        {
            this.addData(list.get(i));
        }

        s_pan = new JScrollPane(table);
        frame.getContentPane().add(s_pan, BorderLayout.CENTER);
        frame.getContentPane().add(pane, BorderLayout.NORTH);
        frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        frame.setSize(frame.getMaximumSize());
        frame.setResizable(false);
        frame.setVisible(true);
    }
    private void addData(User user) {
    model.addRow(user.getLoginName()+"", user.getPassWord()+"",  user.getUserName()+"",user.getPhone()+"");

    }
    private void addUser(){
       model.addRow("loginName","password","userName","phone");
       table.updateUI();
    }
    private void removeData() {
             int a =  table.getSelectedRow();
             String LoginName = (String) model.getValueAt(a,0);
             UserControl.delete(UserControl.findByLoginName(LoginName));
             model.removeRow(a);
             table.updateUI();
    }
    private void saveData() {
       Integer row = table.getSelectedRow();
       String loginName = (String) model.getValueAt(row,0);
       String passWord = (String) model.getValueAt(row,1);
       String userName = (String) model.getValueAt(row,2);
       String phone = (String) model.getValueAt(row,3);
       User user = new  User();
       user.setLoginName(loginName);
       user.setPassWord(passWord);
       user.setPhone(phone);
       user.setUserName(userName);
       user.setStatus("2");
       UserControl.register(user);
    }

    public static void main(String args[]) {
        new UserManagerViewT();
        System.out.println("UserManagerViewT()");
    }
}

class UserManagerTable_Model extends AbstractTableModel {

    private static final long serialVersionUID = -7495940408592595397L;

    private Vector content = null;

    private String[] title_name = { "LOGIN NAME", "PASSWORD", "NAME", "PHONE"};

    public UserManagerTable_Model() {
        content = new Vector();
    }

    public UserManagerTable_Model(int count) {
        content = new Vector(count);
    }

    public void addRow(String loginName,String passWord,String name,String phone) {
        Vector v = new Vector(4);
        v.add(0, loginName);
        v.add(1, passWord);
        v.add(2, name);
        v.add(3, phone);
        content.add(v);
    }

    public void removeRow(int row) {
        content.remove(row);
    }

    public void removeRows(int row, int count) {
        for (int i = 0; i < count; i++) {
            if (content.size() > row) {
                content.remove(row);
            }
        }
    }
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    public void setValueAt(Object value, int row, int col) {
        ((Vector) content.get(row)).remove(col);
        ((Vector) content.get(row)).add(col, value);
        this.fireTableCellUpdated(row, col);
    }

    public String getColumnName(int col) {
        return title_name[col];
    }

    public int getColumnCount() {
        return title_name.length;
    }

    public int getRowCount() {
        return content.size();
    }

    public Object getValueAt(int row, int col) {
        return ((Vector) content.get(row)).get(col);
    }

    public Class getColumnClass(int col) {
        return getValueAt(0, col).getClass();
    }
}
